#!/usr/bin/python

# Python script for Nepomuk firefox

#Get the URL of current Web page

import logging
logger = logging.getLogger("mgrTags")


logger.debug("Script mgrTags.py in the position")


current_url = window.opener.content.document.location.href
current_title = window.opener.content.document.title


tags_synchronizer = components.classes["@mozilla.org/TagsSynchronizer;1"].getService(components.interfaces.nsITagsSynchronizer)

LIST_TAGS = document.getElementById("tagTree")



def getPageInfo():

	try:
		watchedGetPageInfo()
	except Exception, e:
		PROMPTS.alert(window, "Error", "Problem accessing the needed elements to use Nepomuk Foxtrot. Please check that nepomukserver is up and running (Error is \"%s\")." % e)
		window.close()



def watchedGetPageInfo():
	# attach TheNepomukWhisperer to the document
	from the_nepomuk_whisperer import TheNepomukWhisperer
	document.NEPO = TheNepomukWhisperer()
	#Disable the add tag button
	document.getElementById("btAddTag").disabled = True
	
	#Show the info in the interface
	document.getElementById("texturl").setAttribute("value",current_url)
	document.getElementById("texttitle").setAttribute("value",current_title)
	
	#Get Tags of current page
	getTags()


def removeCurrent():
	
	#need some improvement
	tag_label = LIST_TAGS.selectedItem.getAttribute("value")
	
	document_uri = document.NEPO.interface.getAntecedent(PIMO_GROUNDINGOCCURRENCE, current_url)
	if document_uri == None:
		return 1
	
	document.NEPO.nao.removeTag(document_uri, tag_label)
	
	logger.debug("The tag "+tag_label+" has been deleted!")
	getTags()
	
	# Synchronize, so that this tag will also be removed from firefox.
	tags_synchronizer.synchronize("False", "None")


def clearText():
	#Does not work fine
	logger.debug("clearing text")
	blankText=""
	document.getElementById("texttagname").value=""
	document.getElementById("texttagdesc").value=""


def checkName():
	#disable the add tag button if no name entered
	document.getElementById("btAddTag").disabled = (document.getElementById("texttagname").value=="")


def saveTag():
	
	try:
		watchedSaveTag()
	except CanNotException, e:
		PROMPTS.alert(window, "Error", str(e))


#save the tag
def watchedSaveTag():
	
	#Get the values
	page2tag = current_url
	tag2save = document.getElementById("texttagname").value
	desc2save = document.getElementById("texttagdesc").value
	
	document_uri = document.NEPO.interface.getAntecedent(PIMO_GROUNDINGOCCURRENCE, current_url)
	if document_uri != None:
		logger.debug("The document %s has %s as its groundingOccurrence." % (document_uri, current_url))
	else:
		logger.debug("The page \"%s\" has no groundingOccurrence, creating one." % current_url)
		document_uri = document.NEPO.interface.registerAndReify(current_url, NFO_WEBSITE, current_title)
	
	document.NEPO.nao.addTag(document_uri,tag2save,desc2save)
	logger.debug('The tag '+tag2save+' is added')
	
	#Erase the text 
	clearText()
	
	#Get Tags of current page
	getTags()
	
	# Synchronize, so that this tag will also be added in firefox.
	tags_synchronizer.synchronize("None", "True")


def getTags():
	
	document_uri = document.NEPO.interface.getAntecedent(PIMO_GROUNDINGOCCURRENCE, current_url)
	if document_uri == None:
		return 1
	
	tags = document.NEPO.nao.getAllTagsSet()
	
	while LIST_TAGS.childNodes.length != 1:
		LIST_TAGS.removeChild(LIST_TAGS.lastChild)
	
	for tag in tags:
		
		(uri, tag_label, title, desc) = tag
		if uri == current_url:
			row = document.createElement('listitem')
			row.setAttribute('value', tag_label)
			row.setAttribute('tooltiptext', desc)
			if len(desc) >10:
				desc = desc[0:9]+"..."
			cell = document.createElement('listcell')
			cell.setAttribute('label', tag_label)
			row.appendChild(cell)

			cell = document.createElement('listcell')
			cell.setAttribute('label', desc)
			row.appendChild(cell)

			LIST_TAGS.appendChild(row);
			


